<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>微博首页</h1>
<div>
    <div v-if="user.nickname!=null">
        <h2>欢迎{{user.nickname}}回来!</h2>
        <a href="/send.html">发布微博</a>
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <div v-else>
        <a href="/reg.html">注册</a>
        <a href="/log.html">登录</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <h3>
            <a :href="'/detail.html?id='+weibo.id">{{weibo.nickname}}说:{{weibo.content}}</a>
            <span>{{weibo.created}}</span>
        </h3>
    </div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
 <script>
  let v = new Vue({
     el:"body>div",
     data:{
        user:{},
         arr:{}
     },
     methods:{
         logout(){
             axios.get("/logout").then(function (response) {
                 //修改user的值 页面会跟着发生改变
                 if(confirm("是否退出登录?")){
                 v.user={};
                 }
             })
         }
     },
      created:function () {
         //发请求获取当前登录的用户对象
         axios.get("/currentUser").then(function (response) {
             //alert("user="+response.data);
             v.user=response.data;

         })
          //发请求获取微博信息
          axios.get("/weibo/select").then(function (response) {
              v.arr=response.data;

          })
      }
   })
   </script>
</body>
</html>